<template>
  <el-dialog
    :title="!dataForm.id ? '发布Banner' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter="dataFormSubmit()" label-width="100px">
    <el-form-item label="主键id" prop="id" v-if="dataForm.id">
        <el-input v-model="dataForm.id" placeholder="主键id" disabled></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="轮播图" prop="image">
        <el-upload 
              :action="uploadFileUrl" 
              list-type="picture-card" 
              :multiple=false
              :class="{hide:hideUpload}"
              :on-preview="handlePreview"
              :on-remove="remove"
              :on-change="uploadChange"
              :http-request="headUpload">
              <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible="previewVisible">
            <img width="100%" :src="previewPath" alt="">
        </el-dialog>
        <!-- <el-input v-model="dataForm.image" placeholder="banner图片"></el-input> -->
    </el-form-item>
    <el-form-item label="跳转链接" prop="url">
        <el-input v-model="dataForm.url" placeholder="跳转链接"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/smart/oilbanner'
    import {uploadFile} from '@/api/smart/oilfile'

    export default {
    data () {
      return {
        uploadFileUrl: '/smart/oilfile/upload', //图片上传URL地址
        previewPath: '',
        previewVisible: false,
        limitCount: 1,
        hideUpload: false,
        headFile: '',
        headFileList: '',
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          title: '',
          image: '',
          url: '',
          createBy: '',
          updateBy: '',
          createTime: '',
          updateTime: '',
        },
        //表单校验规则
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],

          image: [
            { required: true, message: 'banner图片不能为空', trigger: 'blur' }
          ],

          url: [
            { required: true, message: 'banner链接不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      // 处理图片预览效果
      handlePreview(file) {
            this.previewPath = file.url;
            this.previewVisible = true
      },
      remove(file, fileList) {
            if(fileList.length == 0){
                this.dataRule.image = [{ required: true, message: '请上传图片', trigger: 'change'}];
            }
            this.hideUpload = fileList.length >= this.limitCount;
      },
      uploadChange(file, fileList) {
            this.headFile = file;
            this.headFileList = fileList;
            if(fileList.length==1) {
                let {headImg, ...data} = this.kfFormRules;
                this.kfFormRules = data;
            }
            this.$refs.kfFormRef.clearValidate('headImg');
            this.hideUpload = fileList.length >= this.limitCount;
      },
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            // this.$refs['dataForm'].resetFields()
            this.dataForm.id = ''
            this.dataForm.createBy = ''
            this.dataForm.updateBy = ''
            this.dataForm.createTime = ''
            this.dataForm.updateTime = ''
            if (id) {
              getObj(id).then(response => {
                  this.dataForm = response.data.data
            })
          }
        }) 
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
